package com.example.zjsr.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.zjsr.common.ServerResponse;
import com.example.zjsr.domain.User;
import com.example.zjsr.service.InfoService;
import com.example.zjsr.util.JwtUtil;
import com.example.zjsr.util.RedisUtil;
import org.apache.catalina.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping(value = "/info")
public class InfoController {

    private Logger logger= LoggerFactory.getLogger(InfoController.class);


    @Autowired
    public InfoService infoService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 获取到所有的主播信息
     * @param data
     * @return
     */
    @RequestMapping(value = "/getAnchors",method = RequestMethod.POST)
    public ServerResponse getAnchors(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject= JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String keyword=jsonObject.getString("keyword");
        return infoService.getAnchors(pageNum,pageSize,keyword);
    }


    /**
     * 根据主播ID来获取到主播的详细信息
     * @param data
     * @param request
     * @return
     */
    @RequestMapping(value = "/getAnchorsInfo",method = RequestMethod.POST)
    public ServerResponse getAnchorsInfo(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject= JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String anchorID=jsonObject.getString("anchorID");
        return infoService.getAnchorsInfo(pageNum,pageSize,anchorID);
    }

    /**
     * 获取到主播信息绘制图表
     * @param request
     * @param data
     * @return
     */
    @RequestMapping(value = "/getAnchorsCharts",method = RequestMethod.POST)
    public ServerResponse getAnchorsCharts(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject= JSON.parseObject(data);
        String anchorID=jsonObject.getString("anchorID");
        return infoService.getAnchorsCharts(anchorID);
    }


    /**
     * 获取水果信息种类
     * @param request
     * @param data
     * @return
     */
    @RequestMapping(value = "/getFruit",method = RequestMethod.POST)
    public ServerResponse getFruit(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/

        JSONObject jsonObject=JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String keyword=jsonObject.getString("keyword");
        return infoService.getFruit(pageNum,pageSize,keyword);
    }


    /**
     * 根据水果种类编号获取水果的信息图标
     * @param request
     * @param data
     * @return
     */
    @RequestMapping(value = "/getFruitChart",method = RequestMethod.POST)
    public ServerResponse getFruitChart(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject=JSON.parseObject(data);
        Integer fruitId=jsonObject.getInteger("fruitId");
        return infoService.getFruitChart(fruitId);
    }


    /**
     * 根据水果类别编号获取水果信息
     * @param request
     * @param data
     * @return
     */
    @RequestMapping(value = "/getFruitInfo",method = RequestMethod.POST)
    public ServerResponse getFruitInfo(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject=JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer fruitId=jsonObject.getInteger("fruitId");
        Integer status=jsonObject.getInteger("status");
        return infoService.getFruitInfo(pageNum,pageSize,fruitId,status);
    }


    /**
     * 根据水果类别编号获取水果信息(去空)
     * @param request
     * @param data
     * @return
     */
    @RequestMapping(value = "/getFruitInfoEmpty",method = RequestMethod.POST)
    public ServerResponse getFruitInfoEmpty(HttpServletRequest request,@RequestBody String data){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject=JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer fruitId=jsonObject.getInteger("fruitId");
        return infoService.getFruitInfoEmpty(pageNum,pageSize,fruitId);
    }


    /**
     * 根据字段来获取图表信息
     * @param data
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLivingProduct",method =RequestMethod.POST)
    public ServerResponse getLivingProduct(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
/*        if(!jwt.equals(redisUtil.get(u.getId()+""))){
            return ServerResponse.createByAuthMsg("该账号已经在别处登录！");
        }*/
        JSONObject jsonObject=JSON.parseObject(data);
//        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer fruitId=jsonObject.getInteger("fruitId");
        String[] fruitMapping=jsonObject.getString("fruitMapping").split(",");
        return infoService.getLivingProduct(fruitId,fruitMapping);
    }


    /**
     * 根据水果编号来获取到水果编号的背景图片
     * @param data
     * @return
     */
    @RequestMapping(value = "/getFruitPicture",method = RequestMethod.POST)
    public ServerResponse getFruitPicture(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer fruitId=jsonObject.getInteger("fruitId");
        return infoService.getFruitPicture(fruitId);
    }
}
